function arraySearch(lmnt, arr)
{
	for(var i = 0, iLength = arr.length; i < iLength; i ++)
	{
		if(lmnt == arr[i])
		{
			return i;
		}
	}
	return false;
}

// ------------------------------------------------------------------------------------

$(function()
{
	$('input.i-check-all').click(function(){$(this).parents('table.t-data-edit').find('input[type=checkbox]').attr('checked', this.checked)});

	$('table.t-data-edit tr:even').addClass('tr-dark');

	/*
	 * Функционал картинок
	 * /
	var ulAll = $("#ulAll");
	var ulAllItems = $("#ulAll").find('li');
	var ulSelected = $("#ulSelected");
	var liAll = $('ul.ul-files li');

	liAll
	.click(function()
	{
		var me = $(this);
		selectedId = me.find('input').val();
		liAll.removeClass('li-selected');
		me.addClass('li-selected');
	})
	.dblclick(function()
	{
		var me = $(this);
		switch(me.parent().attr('id'))
		{
			case 'ulAll':
				me.appendTo(ulSelected);
			break;

			case 'ulSelected':
				me.appendTo(ulAll);
			break;
		}
	}).css({cursor:'pointer'});

	$('#linkEdit').mouseover(function()
	{
		var chapter = window.location.href.split('/')[3];
		chapter = chapter.replace('Files', '');
		if(selectedId)
		{
			this.href = '/' + chapter + 'Files/' + 'edit/' + selectedId + '/';
		}
		else
		{
			this.href = '#';
		}
	});

	/*
	 * Фильтр для текстовый строк
	 */
	var tDataRows = $('table.t-data tbody tr');

	$('table.t-data thead input:text[name^=filter]').each(function()
	{
		var me = $(this);
		var td = me.parent();
		var tds = td.parent().find('td');
		this.position = arraySearch(td.get(0), tds);
	}).keyup(function(event)
	{
		if(this.position)
		{
			if(27 == event.keyCode)
			{
				this.value = '';
			}

			var value = this.value.toLowerCase();
			for(var i = 0, iLength = tDataRows.length; i < iLength; i++)
			{
				var row = tDataRows[i];
				if(!row.tds)
				{
					row.tds = $(row).find('td');
				}

				var html = row.tds.get(this.position).innerHTML.toLowerCase();
				if(-1 != html.indexOf(value))
				{
					$(row).removeClass('g-hidden');
				}
				else
				{
					$(row).addClass('g-hidden');
				}
			}
		}
	});

	/*
	 * Фильтр для картинок
	 */
	$('table.t-data input.i-filter-files').keyup(function(event)
	{
		if(27 == event.keyCode)
		{
			this.value = '';
		}

		var value = this.value.toLowerCase();

		for(var i = 0, iLength = ulAllItems.length; i < iLength; i++)
		{
			var item = ulAllItems.get(i);
			var html = item.title.toLowerCase();

			if(-1 != html.indexOf(value))
			{
				$(item).removeClass('g-hidden');
				item.style.position = '';
			}
			else
			{
				$(item).addClass('g-hidden');
				item.style.position = 'absolute';
				item.style.top = '-777px';
			}
		}
	});

	/*
	* Select => Combo
	*/
	$('select.x-combo').each(function()
	{
		$(this).sexyCombo();
	});
	
	
	
	// TinyMCE plugin

	(function() {

		if(typeof tinymce == 'undefined')
		{
			return;
		}
	
		var photosDir = 'http://' + window.location.host.replace('admin.', '') + '/photos/';

		tinymce.create('tinymce.plugins.SDPsMCodePlugin', {
			init : function(ed, url) {
				var t = this;

				ed.onBeforeSetContent.add(function(ed, o) {
					o.content = t['_code2html'](o.content);
				});

				ed.onPostProcess.add(function(ed, o) {
					if (o.set)
						o.content = t['_code2html'](o.content);

					if (o.get)
						o.content = t['_html2code'](o.content);
				});
			},

			// Private methods

			// HTML -> Code
			_html2code : function(s) {
				s = tinymce.trim(s);

				function rep(re, str) {
					s = s.replace(re, str);
				};

				rep(new RegExp('<img class="i-photo" src="' + photosDir  + '(.*?)".*?/>', 'ig'),"[img]$1[/img]");
				

				return s; 
			},

			// Code -> HTML
			_code2html : function(s) {
				s = tinymce.trim(s);

				function rep(re, str) {
					s = s.replace(re, str);
				};
				
				rep(/\[img\](.*?)\[\/img\]/gi,'<img class="i-photo" src="' + photosDir + '$1" alt="" />');
//				rep(/\[photo\](.*?)\[\/photo\]/gi,'<a class="x-hash" href="' + photosDir + '$1"><img class="i-photo" src="' + photosDir + '$1" alt="" /></a>');

				return s; 
			}
		});

		// Register plugin
		tinymce.PluginManager.add('sdpsmcode', tinymce.plugins.SDPsMCodePlugin);
	})();
	
});

// ------------------------------------------------------------------------------------